#include "dlgmain.h"
#include "ui_dlgmain.h"
#include "tmglobaldef.h"

DlgMain::DlgMain(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DlgMain)
{
    ui->setupUi(this);

    //去掉窗口右上角的问号
    Qt::WindowFlags flags = this->windowFlags();
    flags |= Qt::WindowMinimizeButtonHint;
    flags &= ~Qt::WindowCloseButtonHint;
    this->setWindowFlags(flags);
    this->setFixedSize(320,210);

    // 1. 设置当前运行路径
    g_strRootDir = QCoreApplication::applicationDirPath();
    QDir::setCurrent(g_strRootDir);

    // 2. 托盘图标
    m_pSystemTray = new QSystemTrayIcon(this);
    m_pSystemTray->setToolTip(this->windowTitle());
    m_pSystemTray->setIcon(this->windowIcon());
    m_pSystemTray->show();
    connect(m_pSystemTray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayicon_activated(QSystemTrayIcon::ActivationReason)));

    // 3. 启动
    QTimer::singleShot(200, this, SLOT(start_frame()));
}

DlgMain::~DlgMain()
{
    delete ui;
}

void DlgMain::start_frame(){
    this->hide();

    // Clear
    bool bNeedClear = true;
    {
        FILE* pClearF = fopen("./Tmp/clear", "rb");
        char buffTm[56];
        int nRealSize = 0;
        if( pClearF ){
            nRealSize = fread(buffTm, 1, 56, pClearF);
            fclose(pClearF);
        }

        QDateTime curTm = QDateTime::currentDateTime();
        QDateTime lastTm = QDateTime::fromString(QString::fromUtf8(buffTm, nRealSize), "yyyy/MM/dd hh:mm:ss");

        if( lastTm.isValid() && lastTm.daysTo(curTm) < 5 ){
            bNeedClear = false;
        }
        if( !lastTm.isValid() ){
            bNeedClear = true;
        }

        if( bNeedClear ){
            FILE* pClearF = fopen("./Tmp/clear", "wb");
            if( pClearF ){
                QByteArray bCurTm = curTm.toString("yyyy/MM/dd hh:mm:ss").toUtf8();
                fwrite(bCurTm.data(), 1, bCurTm.size(), pClearF);
                fclose(pClearF);
            }
        }
    }

    if(bNeedClear){
        g_clearDirFile("./Log/", "log");
        g_clearDirFile("./Tmp/tts", "wav");
    }
    // 2. 初始化日志和配置文件
    // 更新了全局RobotType和Version
    g_pSettingCore = new QSettings("./Config/Robot.ini", QSettings::IniFormat);
    g_pSettingCore->setIniCodec("UTF-8");
    g_pCodecASCII = QTextCodec::codecForName("GB18030");
    g_pLog = new TmLog();

    g_strRobotType = g_pSettingCore->value("Core/Type").toString();
    g_strCoreVersion = g_pSettingCore->value("Core/Version").toString();
    g_bEnableScreenEx = g_pSettingCore->value("ScreenEx/Enable", 0).toInt()==0?false:true;

    g_pLog->log_I(QString("Loader start: %1 %2").arg(g_strRobotType).arg(g_strRootDir));

    // 3. 初始化Radar
    // 更新了全局MAC
    g_pRadar = new TmRadar();
    g_pRadar->StartRadar();

    // 4. 启动和配置IceNode
    // ICE相关的初始化操作
    // 完成后，要等待10S以后再进行和RPC相关内容
    g_pIceLoader = new TmIceLoader();
    g_pIceLoader->Init();
}


void DlgMain::on_btn_hide_clicked()
{
    this->hide();
}

void DlgMain::on_btn_exit_clicked()
{
    if( g_pIceLoader ){
        g_pIceLoader->StopAll();
    }
    int nLoop = 80;
    while(nLoop>0){
        nLoop -= 1;
        QCoreApplication::processEvents();
        QThread::msleep(10);
    }
    this->close();
}

void DlgMain::trayicon_activated(QSystemTrayIcon::ActivationReason reason)
{
    if( reason == QSystemTrayIcon::DoubleClick || reason == QSystemTrayIcon::Context ){
        if( this->isVisible() ){
            this->hide();
            //托盘显示提示信息
            m_pSystemTray->showMessage(QString("Tami.RobotCore信息:"), QString("窗口已经隐藏..."), QSystemTrayIcon::Information, 2000);
        }else{
            this->show();
            m_pSystemTray->showMessage(QString("Tami.RobotCore信息:"), QString("窗口已经显示..."), QSystemTrayIcon::Information, 2000);
        }
    }

}

void DlgMain::on_btn_link_clicked()
{
    QString strFullPath = QCoreApplication::applicationFilePath();
    // 桌面快捷方式
    QFile::link(strFullPath, QStandardPaths::writableLocation(QStandardPaths::DesktopLocation).append("/").append("Tami.RobotCore.lnk"));
    // ApplicationsLocation
    //QFile::link(strFullPath, QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation).append("/").append("Tami.RobotCore.lnk"));
}
